/****************************************
Figure A.12, Figure 2, Figure A.11
*****************************************/

use "$dir/Data/Final/Isonymy_sy", replace

/**************************************
Figure A.12
**************************************/

preserve 

drop if year >= 1940
collapse ///
        (mean) IsoNonrand_sy IsoObs_sy ///
		 (rawsum) N_y = N_sy [aw = N_sy], by(year)
		 
gen cm = 4*max(0, IsoNonrand_sy)	

binscatter cm year, ///
linetype(connect)  ylabel(0(0.01)0.08) xlabel(1750(38)1940) ///
lcolors(black) ytitle("Cousin marriage rates in the U.S.") xtitle("Year") msymbols(none)
graph export "$dir/Output/TablesFigures/MRrates.png", replace

restore

/**************************************
Figure 2
**************************************/

preserve

* Decade bins

drop if year >= 1940
gen decade_bin = year >= 1750 & year <= 1800
replace decade_bin = 2 if year >=1801 & year<= 1850
replace decade_bin = 3 if year >= 1851 & year <= 1900
replace decade_bin = 4 if year >= 1901 & year <= 1940

* Converting everything to percentages
replace IsoObs_sy = 100*IsoObs_sy
replace IsoNonrand_sy = 100*IsoNonrand_sy

collapse ///
         (mean) IsoObs_sy  IsoNonrand_sy ///
		 (rawsum) N_sd = N_sy [aw = N_sy], by(decade_bin state statename)
		 
gen cm = 4*max(0, IsoNonrand_sy)	

drop if N_sd < 100

* Value lables to decade bin
tostring decade_bin, replace
replace decade_bin = "1750-1800" if decade_bin == "1"
replace decade_bin = "1801-1850" if decade_bin == "2"
replace decade_bin = "1851-1900" if decade_bin == "3"
replace decade_bin = "1901-1940" if decade_bin == "4"


levelsof decade_bin, local(levels)
foreach l of local levels{
maptile cm if decade_bin == "`l'", ndfcolor(black) geog(state) cutvalues(0(2)10) twopt(legend(lab(2 "0%") lab(3 "0%-2%") lab(4 "2%-4%") lab(5 "4%-6%") ///
            lab(6 "6%-8%") lab(7 "8%-10%")  lab(8 ">10%")) title("`l'")) geofolder("$dir/Data/Original/geo_state")
            graph save "$dir/Output/TablesFigures/CM_Rate `l'.gph", replace
}

gr combine "$dir/Output/TablesFigures/CM_Rate 1750-1800.gph" "$dir/Output/TablesFigures/CM_Rate 1801-1850.gph" "$dir/Output/TablesFigures/CM_Rate 1851-1900.gph" ///
"$dir/Output/TablesFigures/CM_Rate 1901-1940.gph", title("Cousin marriage rates in the U.S.") ///
cols(2)  iscale(0.5)
graph export "$dir/Output/TablesFigures/Fig_CMRates.png", replace

restore

/**************************************
Figure A.11
**************************************/

drop if year > 1858

replace IsoObs_sy = 100*IsoObs_sy
replace IsoNonrand_sy = 100*IsoNonrand_sy

collapse ///
         (mean) IsoNonrand_s = IsoNonrand_sy IsoObs_s = IsoObs_sy ///
		 (rawsum) N_s = N_sy [aw = N_sy], by(state statename)
		 
gen cm = 4*max(0, IsoNonrand_s)	
drop if N_s < 100

maptile cm, ndfcolor(black) geog(state) cutvalues(0(2)10) twopt(legend(lab(2 "0%") lab(3 "0%-2%") lab(4 "2%-4%") lab(5 "4%-6%") ///
            lab(6 "6%-8%") lab(7 "8%-10%")  lab(8 ">10%")) title("Cousin marriage rates in the pre-period (1750-1858)")) ///
			geofolder("$dir/Data/Original/geo_state")

graph export "$dir/Output/TablesFigures/Fig_CMRates_Prep.png", replace
